package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.practice.dao.DaoFactory;
import org.practice.dao.UserDao;
import org.practice.form.VerifyForm;
import org.practice.model.User;

public class RegisterTwoAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		VerifyForm vf = (VerifyForm)form;
		String verifyCode = vf.getVerifyCode();
		String code ="";
		int id =-1;
		if(verifyCode.contains("-")){
			code = verifyCode.substring(0, verifyCode.lastIndexOf("-"));
			id = Integer.parseInt(verifyCode.substring(verifyCode.lastIndexOf("-") + 1));
		}
		UserDao userDao = DaoFactory.createUserDao();
		User user = userDao.verifyUser(id,code);
		if (user == null) {
			//只有将errors信息保存在request，页面才可以通过以下方式读取<html:errors property=""/>，如果页面使用这种方式就可以直接读<bean:message key=""/>
			ActionMessages errors=new ActionMessages();
			errors.add("error.verify.emailcode",new ActionMessage("error.verify.emailcode"));
			this.saveErrors(request, errors);
			return mapping.findForward("fail");
		}
		return mapping.findForward("success");
	}
	
}
